package com.genesis.yunnanji.view;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.utils.PhoneUtil;
import com.genesis.yunnanji.utils.RegexUtils;
import com.youth.xframe.widget.XStateButton;

import java.util.LinkedHashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2018/9/19 on 16:37
 * 作者:Kite
 * 描述:忘记密码
 */
public class ForgetPasswordActivity extends BaseActivity {

    @BindView(R.id.et_phone)
    EditText etPhone;

    @BindView(R.id.et_code)
    EditText etCode;

    @BindView(R.id.tv_get_code)
    TextView tvGetCode;

    @BindView(R.id.et_password)
    EditText etPassword;

    @BindView(R.id.et_verify_password)
    EditText etVerifyPassword;

    @BindView(R.id.btn_register)
    XStateButton btnRegister;

    private LinkedHashMap<Integer, String> errorMap = new LinkedHashMap<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    public void initView() {
        super.initView();
        setTitle("找回密码");
        new PhoneUtil().phoneEditNum(etPhone);
        editIsEmpty();
    }

    @OnClick({R.id.tv_get_code, R.id.btn_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                getCode(etPhone, tvGetCode, "");
                break;
            case R.id.btn_register:
                break;
        }
    }

    private void editIsEmpty() {
        TextWatcher etPhoneWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null) {
                    String mobile = editable.toString().replace(" ", "");
                    if (!TextUtils.isEmpty(mobile)) {
                        errorMap.remove(1);
                        if (mobile.length() == 11 && RegexUtils.checkMobile(mobile)) {
                            errorMap.remove(2);
                        } else {
                            errorMap.put(2, "请填写正确的手机号码");
                        }
                    } else {
                        errorMap.put(1, "请输入您的手机号码");
                    }
                    changeClickable();
                }
            }
        };
        etPhone.addTextChangedListener(etPhoneWatcher);

        TextWatcher etCodeWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null) {
                    String code = editable.toString();
                    if (!TextUtils.isEmpty(code)) {
                        errorMap.remove(3);
                        if (code.length() >= 4 && code.length() <= 6) {
                            errorMap.remove(4);
                        } else {
                            errorMap.put(4, "请输入正确的验证码");
                        }
                    } else {
                        errorMap.put(3, "请输入验证码");
                    }
                    changeClickable();
                }
            }
        };
        etCode.addTextChangedListener(etCodeWatcher);

        TextWatcher etPasswordWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null) {
                    String code = editable.toString();
                    if (!TextUtils.isEmpty(code)) {
                        errorMap.remove(5);
                        if (code.length() >= 8 && code.length() <= 16) {
                            errorMap.remove(6);
                        } else {
                            errorMap.put(6, "请输入正确的密码");
                        }
                    } else {
                        errorMap.put(5, "请输入密码");
                    }
                    changeClickable();
                }
            }
        };
        etPassword.addTextChangedListener(etPasswordWatcher);

        TextWatcher etVerifyPasswordWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null) {
                    String code = editable.toString();
                    if (!TextUtils.isEmpty(code)) {
                        errorMap.remove(7);
                        if (code.length() >= 8 && code.length() <= 16) {
                            errorMap.remove(8);
                        } else {
                            errorMap.put(8, "请输入正确的密码");
                        }
                    } else {
                        errorMap.put(7, "请输入密码");
                    }
                    changeClickable();
                }
            }
        };
        etVerifyPassword.addTextChangedListener(etVerifyPasswordWatcher);

        errorMap.put(3, "请输入您的验证码");
        errorMap.put(1, "请输入您的手机号码");
        errorMap.put(5, "请输入您的登录密码");
        errorMap.put(7, "请输入您的登录密码");

        btnRegister.setClickable(false);
        btnRegister.setNormalBackgroundColor(getResources().getColor(R.color.color_ccc));
    }

    public void changeClickable() {
        if (errorMap.isEmpty()) {
            btnRegister.setClickable(true);
            btnRegister.setNormalBackgroundColor(getResources().getColor(R.color.colorTheme));
        } else {
            btnRegister.setClickable(false);
            btnRegister.setNormalBackgroundColor(getResources().getColor(R.color.color_ccc));
        }
    }

}
